package com.ruoyi.rural.controller;

import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.DictUtils;
import com.ruoyi.rural.domain.*;
import com.ruoyi.rural.service.*;
import com.ruoyi.system.api.domain.SysDictData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/frontPage")
public class FrontPageController extends BaseController {

    @Autowired
    private IRuralUserService ruralUserService;

    @Autowired
    private IRuralHouseService ruralHouseService;

    @Autowired
    private IRuralVaccinationService ruralVaccinationService;

    @Autowired
    private IRuralBenefitAnagementDonateService ruralBenefitAnagementDonateService;

    @Autowired
    private IRuralApplicationBenefitAnagementService ruralApplicationBenefitAnagementService;

    @Autowired
    private IRuralBenefitAnagementService ruralBenefitAnagementService;

    @Autowired
    private IRuralPovertyAlleviationMaterialsService ruralPovertyAlleviationMaterialsService;

    @Autowired
    private IRuralPovertyAlleviationMaterialsDonateService ruralPovertyAlleviationMaterialsDonateService;

    @Autowired
    private IRuralApplicationEpidemicPreventionMaterialsService ruralApplicationEpidemicPreventionMaterialsService;

    @Autowired
    private IRuralApplicationPovertyAlleviationMaterialsService ruralApplicationPovertyAlleviationMaterialsService;

    @Autowired
    private IRuralVirusPreventionMaterialsService ruralVirusPreventionMaterialsService;

    @Autowired
    private IRuralVirusPreventionMaterialsDonateService ruralVirusPreventionMaterialsDonateService;


    @GetMapping("/frontPageData")
    public AjaxResult frontPageData(){
        Map<String, Object> data = new HashMap<>();
        List<RuralUser> ruralUsers = ruralUserService.selectRuralUserList(new RuralUser());
        data.put("userCount",ruralUsers.size());
        List<RuralHouse> ruralHouses = ruralHouseService.selectRuralHouseList(new RuralHouse());
        data.put("houseCount",ruralHouses.size());

        List<VaccinationData> completedVaccination= ruralVaccinationService.selectRuralVaccinationListByCompleted();
        List<VaccinationData> undoneVaccination= new ArrayList<>();

        for (VaccinationData vaccinationData : completedVaccination) {
            VaccinationData vaccinationData1 = new VaccinationData();
            vaccinationData1.setVaccinationDoses(vaccinationData.getVaccinationDoses());
            vaccinationData1.setCount(ruralUsers.size() - vaccinationData.getCount());
            undoneVaccination.add(vaccinationData1);
        }
        data.put("completedVaccination",completedVaccination);
        data.put("undoneVaccination",undoneVaccination);

        List<RuralApplicationBenefitAnagement> ruralApplicationBenefitAnagements = ruralApplicationBenefitAnagementService.selectRuralApplicationBenefitAnagementList(new RuralApplicationBenefitAnagement());
        List<RuralBenefitAnagementDonate> ruralBenefitAnagementDonates = ruralBenefitAnagementDonateService.selectRuralBenefitAnagementDonateList(new RuralBenefitAnagementDonate());

        Map<String, Object> alms = new HashMap<>();
        alms.put("applicationCumulativeCount",ruralApplicationBenefitAnagements.size());
        alms.put("donationCumulativeCount",ruralBenefitAnagementDonates.size());
        BigDecimal applicationCumulativeAmount = new BigDecimal("0");
        BigDecimal donationCumulativeAmount = new BigDecimal("0");
        for (RuralApplicationBenefitAnagement ruralApplicationBenefitAnagement : ruralApplicationBenefitAnagements) {
            applicationCumulativeAmount.add(new BigDecimal(ruralApplicationBenefitAnagement.getBenefitAmount()));
        }
        for (RuralBenefitAnagementDonate ruralBenefitAnagementDonate : ruralBenefitAnagementDonates) {
            donationCumulativeAmount.add(new BigDecimal(ruralBenefitAnagementDonate.getBenefitAmount()));
        }
        alms.put("applicationCumulativeAmount",applicationCumulativeAmount.toString());
        alms.put("donationCumulativeAmount",donationCumulativeAmount.toString());

        List<RuralApplicationBenefitAnagement> ruralApplicationBenefitAnagementsByWeek = ruralApplicationBenefitAnagementService.selectRuralApplicationBenefitAnagementListByWeek(new RuralApplicationBenefitAnagement());
        List<RuralBenefitAnagementDonate> ruralBenefitAnagementDonatesByWeek = ruralBenefitAnagementDonateService.selectRuralBenefitAnagementDonateListByWeek(new RuralBenefitAnagementDonate());

        alms.put("applicationCumulativeCountByWeek",ruralApplicationBenefitAnagementsByWeek.size());
        alms.put("donationCumulativeCountByWeek",ruralBenefitAnagementDonatesByWeek.size());
        BigDecimal applicationCumulativeAmountByWeek = new BigDecimal("0");
        BigDecimal donationCumulativeAmountByWeek = new BigDecimal("0");
        for (RuralApplicationBenefitAnagement ruralApplicationBenefitAnagement : ruralApplicationBenefitAnagementsByWeek) {
            if (StringUtils.isNotEmpty(ruralApplicationBenefitAnagement.getBenefitAmount())){
                applicationCumulativeAmountByWeek.add(new BigDecimal(ruralApplicationBenefitAnagement.getBenefitAmount()));
            }
        }
        for (RuralBenefitAnagementDonate ruralBenefitAnagementDonate : ruralBenefitAnagementDonatesByWeek) {
            if (StringUtils.isNotEmpty(ruralBenefitAnagementDonate.getBenefitAmount())){
                donationCumulativeAmountByWeek.add(new BigDecimal(ruralBenefitAnagementDonate.getBenefitAmount()));
            }
        }
        alms.put("applicationCumulativeAmountByWeek",applicationCumulativeAmountByWeek.toString());
        alms.put("donationCumulativeAmountByWeek",donationCumulativeAmountByWeek.toString());

        List<RuralBenefitAnagement> ruralBenefitAnagements = ruralBenefitAnagementService.selectRuralBenefitAnagementList1(new RuralBenefitAnagement());

        alms.put("ruralBenefitAnagements",ruralBenefitAnagements);

        data.put("alms",alms);

        List<RuralApplicationPovertyAlleviationMaterials> ruralApplicationPovertyAlleviationMaterials = ruralApplicationPovertyAlleviationMaterialsService.selectRuralApplicationPovertyAlleviationMaterialsList(new RuralApplicationPovertyAlleviationMaterials());
        List<RuralPovertyAlleviationMaterialsDonate> ruralPovertyAlleviationMaterialsDonates = ruralPovertyAlleviationMaterialsDonateService.selectRuralPovertyAlleviationMaterialsDonateList(new RuralPovertyAlleviationMaterialsDonate());

        Map<String, Object> povertyAlleviationMaterials = new HashMap<>();
        povertyAlleviationMaterials.put("applicationCumulativeCount",ruralApplicationPovertyAlleviationMaterials.size());
        povertyAlleviationMaterials.put("donationCumulativeCount",ruralPovertyAlleviationMaterialsDonates.size());

        List<RuralApplicationPovertyAlleviationMaterials> ruralApplicationPovertyAlleviationMaterialsByWeek = ruralApplicationPovertyAlleviationMaterialsService.selectRuralApplicationPovertyAlleviationMaterialsListByWeek(new RuralApplicationPovertyAlleviationMaterials());
        List<RuralPovertyAlleviationMaterialsDonate> ruralPovertyAlleviationMaterialsDonatesByWeek = ruralPovertyAlleviationMaterialsDonateService.selectRuralPovertyAlleviationMaterialsDonateListByWeek(new RuralPovertyAlleviationMaterialsDonate());

        povertyAlleviationMaterials.put("applicationCumulativeCountByWeek",ruralApplicationPovertyAlleviationMaterialsByWeek.size());
        povertyAlleviationMaterials.put("donationCumulativeCountByWeek",ruralPovertyAlleviationMaterialsDonatesByWeek.size());

        data.put("povertyAlleviationMaterials",povertyAlleviationMaterials);

        Map<String, Object> epidemicPreventionMaterials = new HashMap<>();
        List<RuralApplicationEpidemicPreventionMaterials> ruralApplicationEpidemicPreventionMaterials = ruralApplicationEpidemicPreventionMaterialsService.selectRuralApplicationEpidemicPreventionMaterialsList(new RuralApplicationEpidemicPreventionMaterials());
        List<RuralVirusPreventionMaterialsDonate> ruralVirusPreventionMaterialsDonates = ruralVirusPreventionMaterialsDonateService.selectRuralVirusPreventionMaterialsDonateList(new RuralVirusPreventionMaterialsDonate());

        epidemicPreventionMaterials.put("applicationCumulativeCount",ruralApplicationEpidemicPreventionMaterials.size());
        epidemicPreventionMaterials.put("donationCumulativeCount",ruralVirusPreventionMaterialsDonates.size());

        List<RuralApplicationEpidemicPreventionMaterials> ruralApplicationEpidemicPreventionMaterialsByWeek = ruralApplicationEpidemicPreventionMaterialsService.selectRuralApplicationEpidemicPreventionMaterialsListByWeek(new RuralApplicationEpidemicPreventionMaterials());
        List<RuralVirusPreventionMaterialsDonate> ruralVirusPreventionMaterialsDonatesByWeek = ruralVirusPreventionMaterialsDonateService.selectRuralVirusPreventionMaterialsDonateListByWeek(new RuralVirusPreventionMaterialsDonate());

        epidemicPreventionMaterials.put("applicationCumulativeCountByWeek",ruralApplicationEpidemicPreventionMaterialsByWeek.size());
        epidemicPreventionMaterials.put("donationCumulativeCountByWeek",ruralVirusPreventionMaterialsDonatesByWeek.size());

        data.put("epidemicPreventionMaterials",epidemicPreventionMaterials);

        return AjaxResult.success(data);
    }


}
